<?php
namespace app\logic;

use app\common\corelib\CacheHelper;
use app\models\Address;
use app\models\Favorite;
use app\models\Goods;
use app\models\Tag;
use app\models\User;
use yii\base\Security;
use yii\db\Query;



class UserLogic
{

    public function getFavorite($user_id){
        /*return Goods::find()
            ->select('f.fav_id,g.goods_id,g.goods_pic,g.goods_name,g.simple_desc,g.goods_price')
            ->from('{{%goods}} g')
            ->leftJoin('{{%favorite}} f', 'f.goods_id=g.goods_id')
            ->where(['f.user_id'=>$user_id,'g.status'=>1])
            ->orderBy('f.add_time desc')
            ->asArray()->all();*/
        return Favorite::find()
            ->select('f.fav_id,g.goods_id,g.goods_pic,g.goods_name,g.simple_desc,g.goods_price')
            ->from('{{%favorite}} f')
            ->leftJoin('{{%goods}} g', 'g.goods_id=f.goods_id')
            ->where(['f.user_id'=>$user_id,'g.status'=>1])
            ->orderBy('f.add_time desc')
            ->asArray()->all();
    }

    public function addFavorite($user_id,$goods_id){
        $favorite = new Favorite();
        $favorite->user_id = $user_id;
        $favorite->goods_id = $goods_id;
        $favorite->add_time = date('Y-m-d H:i:s');
        return $favorite->save();
    }

    public function delFavorite($user_id,$fav_id,$goods_id){
        if($fav_id)
            $favorite = Favorite::findOne(['fav_id'=>$fav_id,'user_id'=>$user_id]);
        else
            $favorite = Favorite::findOne(['goods_id'=>$goods_id,'user_id'=>$user_id]);
        return $favorite->delete();
    }

    public function getAddresss($user_id){
        return Address::find()
            ->select('a.addr_id,a.name,a.phone,a1.region_name as province,a2.region_name as city,a3.region_name as district,a.address,a.is_default')
            ->from('{{%address}} a')
            ->leftJoin('{{%area}} a1', 'a.province_code=a1.region_code')
            ->leftJoin('{{%area}} a2', 'a.city_code=a2.region_code')
            ->leftJoin('{{%area}} a3', 'a.district_code=a3.region_code')
            ->where(['a.user_id'=>$user_id])
            ->orderBy('is_default desc, addr_id desc')
            ->asArray()->all();
    }

    public function getAddress($user_id,$addr_id){
        return Address::find()
            ->select('a.addr_id,a.name,a.phone,a.province_code,a1.region_name as province,a.city_code,a2.region_name as city,a.district_code,a3.region_name as district,a.address,a.is_default')
            ->from('{{%address}} a')
            ->leftJoin('{{%area}} a1', 'a.province_code=a1.region_code')
            ->leftJoin('{{%area}} a2', 'a.city_code=a2.region_code')
            ->leftJoin('{{%area}} a3', 'a.district_code=a3.region_code')
            ->where(['a.user_id'=>$user_id,'addr_id'=>$addr_id])
            ->asArray()->one();
    }

    public function addAddress($user_id,$name,$phone,$province_code,$city_code,$district_code,$address,$is_default){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $result1 = true;
        if($is_default){
            $old_address = Address::findOne(['user_id'=>$user_id,'is_default'=>1]);
            if($old_address){
                $old_address->is_default = 0;
                $result1 &= $old_address->save();
            }
        }
        $new_address = new Address();
        $new_address->user_id = $user_id;
        $new_address->name = $name;
        $new_address->phone = $phone;
        $new_address->province_code = $province_code;
        $new_address->city_code = $city_code;
        $new_address->district_code = $district_code;
        $new_address->address = $address;
        $new_address->is_default = $is_default;
        $result2 = $new_address->save();
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            return false;
        }
    }

    public function editAddress($user_id,$addr_id,$name,$phone,$province_code,$city_code,$district_code,$address,$is_default){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $result1 = true;
        if($is_default){
            $old_address = Address::findOne(['user_id'=>$user_id,'is_default'=>1]);
            if($old_address){
                $old_address->is_default = 0;
                $result1 &= $old_address->save();
            }
        }
        $new_address = Address::findOne(['user_id'=>$user_id,'addr_id'=>$addr_id]);
        $new_address->name = $name;
        $new_address->phone = $phone;
        $new_address->province_code = $province_code;
        $new_address->city_code = $city_code;
        $new_address->district_code = $district_code;
        $new_address->address = $address;
        $new_address->is_default = $is_default;
        $result2 = $new_address->save();
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            return false;
        }
    }

    public function delAddress($user_id,$addr_id){
        $address = Address::findOne(['addr_id'=>$addr_id,'user_id'=>$user_id]);
        return $address->delete();
    }

    public function getDefaultAddress($user_id){
        return Address::find()
            ->select('a.addr_id,a.name,a.phone,a1.region_name as province,a2.region_name as city,a3.region_name as district,a.address,a.is_default')
            ->from('{{%address}} a')
            ->leftJoin('{{%area}} a1', 'a.province_code=a1.region_code')
            ->leftJoin('{{%area}} a2', 'a.city_code=a2.region_code')
            ->leftJoin('{{%area}} a3', 'a.district_code=a3.region_code')
            ->where(['a.user_id'=>$user_id,'is_default'=>1])
            ->asArray()->one();
    }

    public function addUser($mobile,$token,$password){
        $user = new User();
        $user->mobile = $mobile;
        $user->zj_token = $token;
        $user->save();
        return $user->toArray();
    }

    public function updateUser($mobile,$token){
        $user = User::findOne(['mobile'=>$mobile]);
        if($user){
            $user->zj_token = $token;
            $user->save();
        }else{
            $user = new User();
            $user->mobile = $mobile;
            $user->zj_token = $token;
            $user->save();
        }

        return $user->toArray();
    }

    public function getById($user_id){
        return User::findOne($user_id)->toArray();
    }

}